<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户操作页面</title>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" />
    <script type="text/javascript" src="../../res/js/jquery-3.4.1.min.js"></script>
    <script type="text/javascript" src="../../res/layui/layui.js"></script>
    <script type="text/javascript" src="../../res/js/sweetalert2.js"></script>
    <script type="text/javascript" src="../../res/js/tools.js"></script>
</head>
<body>
<script type="text/javascript">
    $.ajaxSetup({
        async: false
    });
    $(function () {
        var url="../../user/roleAll.form";
        $.post(url,null,function (obj) {
            $("#resultAll").html("");
            $.each(obj,function (index,item) {
                $("#resultAll").append('<input type="checkbox" value="'+item.roleId+'" id="'+item.roleId+'" name="roleId" title="'+item.roleName+'">')
            })
            layui.use('element', function() {
                var element = layui.element;
                element.init();
            });
        },"json")

        var userId=getQueryString("userId");
        if(userId){
            var url="../../user/findOne.form";
            var data={userId:userId};
            $.post(url,data,function (obj) {
                $("#userId").val(""+obj.user.userId);
                $("#userName").val(""+obj.user.userName);
                $("#userPwd").val(""+obj.user.userPwd);
                $("#userSort").val(""+obj.user.userSort);
                $("#userPhone").val(""+obj.user.userPhone);
                $("#userMail").val(""+obj.user.userMail);
                $.each(obj.roleOrUser,function (index,item) {
                    console.info(obj.user.userName)
                    $("#"+item.roleId).prop("checked",true);
                })
            },"json")
        }

        $("#userCancel").on("click",function () {
            window.parent.location.reload();//刷新父页面
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            parent.layer.close(index);
        })

    })



</script>
<form class="layui-form" action="" lay-filter="example">
    <input type="hidden" id="userId" name="userId"/>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="userName" id="userName" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="password" name="userPwd" id="userPwd" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-inline">
            <input type="number" name="userSort" id="userSort" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-inline">
                <input type="tel" name="userPhone" id="userPhone" lay-verify="required|phone" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">邮箱-Email</label>
            <div class="layui-input-inline">
                <input type="text" name="userMail" id="userMail" lay-verify="email" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block" id="resultAll">
            <input type="checkbox" name="like[write]" title="写作">
            <input type="checkbox" name="like[read]" title="阅读">
            <input type="checkbox" name="like[daze]" title="发呆">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="button" class="layui-btn layui-btn-primary" value="取消" id="userCancel"/>
            <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
        </div>
    </div>
</form>
<script>
    var form=null;
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });


        //监听提交
        form.on('submit(demo1)', function(data){
            var ids="";
            $("input[name='roleId']:checked").each(function(index,item){
                if($("input[name='roleId']:checked").length==(index+1)){
                    ids+=$(this).val();
                }else{
                    ids+=$(this).val()+",";
                }
            });

            var datas=JSON.stringify(data.field)
            datas=JSON.parse(datas);
            datas.roleId=ids;
            var url="../../user/addOrUpdate.form";
            $.post(url,datas,function (objs) {
                console.info("运算完成")
                if(objs==1){
                    console.info("增加成功")
                    window.parent.location.reload();//刷新父页面
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }
            },"json");
            return false;
        });

    });
</script>
</body>
</html>